<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="180"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">保 存</AjaxButton>
    </div>
  </div>
</template>

<script>
import {
  queryTrackingTask,
  queryTrackingTaskByid,
  addTrackingTask,
  modifyTrackingTask
 } from '@/api/potenCus/basedata/followTask';
import { getDataType } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';

export default {
  name: 'VisitModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      dataType: getDataType('dataType'),
      formList: this.createFormList()
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      console.log('dataType', this.dataType)
      console.log('this.rowData',this.rowData)
      console.log('数据来源', this.type, this.rowData.dataSources)
      this.taskType=70251005
      if( this.dataType !=10461001 ){
        this.formList.find(item => item.fieldName ==='shopIntervalDays').hidden = true
        this.formList.find(item => item.fieldName ==='intervalDays').hidden = false
        this.formList.find(item => item.fieldName ==='intervalDays').label = '间隔天数'
        if((this.type === 'edit')){
          this.formList.forEach(x => {
            x.initialValue = this.rowData[x.fieldName];
          })
        }
      }
      if (this.type === 'edit') {
        this.recordId = this.rowData.taskId;
        this.dataSources = this.rowData.dataSources
        if(this.dataType == 10461001){   //店端
          if( this.dataSources == 10451001 ){  //数据来源为店端
            this.formList.forEach(x => {
              if(x.fieldName ==='shopIntervalDays'){
                x.initialValue = this.rowData['intervalDays'];
              }else {
                x.initialValue = this.rowData[x.fieldName];
              }
            });
          } else {   //来源为厂端
            this.formList.find(item => item.fieldName ==='shopIntervalDays').label = '间隔天数(须小于等于默认值)'
            this.formList.find(item => item.fieldName ==='intervalDays').hidden = false
            this.formList.find(item => item.fieldName ==='intervalDays').disabled = true
            this.formList.find(item => item.fieldName ==='taskCode').disabled = true
            this.formList.find(item => item.fieldName ==='taskName').disabled = true
            this.formList.find(item => item.fieldName ==='taskContent').disabled = true
            this.formList.find(item => item.fieldName ==='isValid').disabled = true
            this.formList.forEach(x =>{
              x.initialValue = this.rowData[x.fieldName];
            })
          }
        }
        // else if(this.dataType !=10461001 ){   //厂端
        //   this.formList.forEach(x => {
        //     x.initialValue = this.rowData[x.fieldName];
        //   })
        // }
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '任务编码',
          fieldName: 'taskCode',
          maxlength: 6,
          placeholder: '请输入',
          rules: [{ required: true, message: '任务编码不能为空', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '任务名称',
          fieldName: 'taskName',
          maxlength: 10,
          placeholder: '请输入',
          rules: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '任务内容',
          fieldName: 'taskContent',
          maxlength: 50,
          placeholder: '请输入',
          rules: [{ required: true, message: '任务内容不能为空', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '间隔天数',
          fieldName: 'shopIntervalDays',
          placeholder: '间隔天数',
          initialValue: '',
          hidden: false,
          rules: [{ required: true, message: '间隔天数不能为空', trigger: 'blur' }],
        },
        {
          type: 'INPUT_NUMBER',
          label: '间隔天数(默认)',
          maxlength: 2,
          fieldName: 'intervalDays',
          placeholder: '请输入',
          hidden: true,
          disabled: false,
          rules: [{ required: true, message: '间隔天数不能为空', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择有效性', trigger: 'change' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      console.log("保存", this.saveData)
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await addTrackingTask({...this.saveData,taskType:this.taskType});
        }
        if (this.type === 'edit') {
          if(this.saveData.intervalDays){
            if(this.saveData.shopIntervalDays > this.saveData.intervalDays){
              this.$message.info('间隔天数须小于等于默认值')
            } else{
              res = await modifyTrackingTask({...this.saveData,taskType:this.taskType,taskId:this.recordId, dataSources:this.dataSources }, this.recordId);
            }
          } else{
            res = await modifyTrackingTask({...this.saveData,taskType:this.taskType,taskId:this.recordId, dataSources:this.dataSources }, this.recordId);
          }

        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
          this.$message.success("保存成功!");
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
  }
};
</script>

<style lang="less" scoped>
</style>
